package com.baizhi.zyj.service;

import com.baizhi.zyj.dao.CategoryMapper;
import com.baizhi.zyj.dto.CategoryAddDto;
import com.baizhi.zyj.dto.CategoryDeleteDto;
import com.baizhi.zyj.dto.CategoryUpdateDto;
import com.baizhi.zyj.entity.Category;
import com.baizhi.zyj.entity.CategoryExample;
import com.baizhi.zyj.vo.CategoryQueryByLevelsVo;
import com.baizhi.zyj.vo.CategoryStaVo;
import com.baizhi.zyj.vo.CategoryVo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{


    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<CategoryQueryByLevelsVo> queryByLevelsCategory(int levels) {
        List<CategoryQueryByLevelsVo> categoryQueryByLevelsVos = new ArrayList<>();
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(levels);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            categoryQueryByLevelsVos.add(new CategoryQueryByLevelsVo(category.getId(), category.getCateName()));
        }
        return categoryQueryByLevelsVos;
    }

    @Override
    public CategoryStaVo update(CategoryUpdateDto categoryUpdateDto) {
        CategoryStaVo categoryStaVo = new CategoryStaVo();
        try {
            Category category = new Category();
            category.setId(categoryUpdateDto.getId());
            category.setCateName(categoryUpdateDto.getCateName());
            category.setParentId(categoryUpdateDto.getParentId());
            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andIdEqualTo(categoryUpdateDto.getId());
            categoryMapper.updateByExampleSelective(category, categoryExample);
            categoryStaVo.setMessage("修改成功");
            categoryStaVo.setStatus(200);
        }catch (Exception e){
            e.printStackTrace();
            categoryStaVo.setMessage("修改失败");
            categoryStaVo.setStatus(400);
        }
        return categoryStaVo;
    }

    @Override
    public CategoryStaVo add(CategoryAddDto categoryAddDto) {
        CategoryStaVo categoryStaVo = new CategoryStaVo();
        try{
            if (categoryAddDto.getParentId()==null){
                categoryMapper.insert(new Category(null,categoryAddDto.getCateName(),1,null));

            }else{
                categoryMapper.insert(new Category(null,categoryAddDto.getCateName(),2,categoryAddDto.getParentId()));
            }
            categoryStaVo.setMessage("message");
            categoryStaVo.setStatus(200);

        }catch (Exception e){
            e.printStackTrace();
            categoryStaVo.setMessage("message");
            categoryStaVo.setStatus(400);
        }
        return categoryStaVo;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Category queryById(int id) {
        Category category = new Category();
        category.setId(id);
       return  categoryMapper.selectOne(category);
    }

    @Override
    public CategoryStaVo delete(CategoryDeleteDto categoryDeleteDto) {
        CategoryStaVo categoryDeleteVo = new CategoryStaVo();
        if (categoryDeleteDto.getParentId()==null){
            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andParentIdEqualTo(categoryDeleteDto.getId());
            int countByExample = categoryMapper.selectCountByExample(categoryExample);
            if (countByExample==0){
                categoryMapper.deleteByPrimaryKey(categoryDeleteDto.getId());
                categoryDeleteVo.setMessage("删除成功");
                categoryDeleteVo.setStatus(200);
            }else {
                categoryDeleteVo.setMessage(" 一级类别下有二级类别不能删除");
                categoryDeleteVo.setStatus(400);
            }
        }else {
            categoryMapper.deleteByPrimaryKey(categoryDeleteDto.getId());
            categoryDeleteVo.setMessage("删除成功");
            categoryDeleteVo.setStatus(200);
        }
        return categoryDeleteVo;
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public CategoryVo queryOnePage(int page, int pageSize) {
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(1);//levels为1
        List<Category> oneCategorys = categoryMapper.selectByExampleAndRowBounds(categoryExample, rowBounds);
        int oneTotal = categoryMapper.selectCountByExample(categoryExample);
        CategoryVo categoryVo = new CategoryVo(page,oneTotal,oneCategorys);
        return categoryVo;
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public CategoryVo queryTwoPage(int page, int pageSize,int parentId) {
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andParentIdEqualTo(parentId);
        List<Category> oneCategorys = categoryMapper.selectByExampleAndRowBounds(categoryExample, rowBounds);
        int oneTotal = categoryMapper.selectCountByExample(categoryExample);
        CategoryVo categoryVo = new CategoryVo(page,oneTotal,oneCategorys);
        return categoryVo;
    }
}
